package com.ruoyi.project.cms.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cms.domain.BizPost;
import com.ruoyi.project.cms.service.IBizPostService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 实习岗位Controller
 *
 * @author ruoyi
 * @date 2023-03-18
 */
@RestController
@RequestMapping("/cms/post")
public class BizPostController extends BaseController
{
    @Autowired
    private IBizPostService bizPostService;

    @Autowired
    private SysPermissionService sysPermissionService;

    /**
     * 查询实习岗位列表
     */
    @PreAuthorize("@ss.hasPermi('cms:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPost bizPost)
    {
        startPage();
        //1.如果角色为学生,则只查看开放的岗位
        //2.如果角色为企业,则只查自己法不的岗位
        SysUser user = SecurityUtils.getLoginUser().getUser();

        Set<String> rolePermission = sysPermissionService.getRolePermission(user);




        Long roleId = user.getRoleId();
//        100,学生角色
//        101,企业角色
       // System.out.println(roleId);
        if(roleId!=null){
        if(roleId.equals(100L)){
            bizPost.setState(0);
        }else if(roleId.equals(101L)){
            bizPost.setUserId(user.getUserId());
        }
        }

        // company student
        if(rolePermission.contains("student")){
            System.out.println("student");
            bizPost.setState(0);
        }else if(rolePermission.contains("company")){
            System.out.println("company");
            bizPost.setUserId(user.getUserId());
        }
        List<BizPost> list = bizPostService.selectBizPostList(bizPost);
        return getDataTable(list);
    }

    /**
     * 导出实习岗位列表
     */
    @PreAuthorize("@ss.hasPermi('cms:post:export')")
    @Log(title = "实习岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPost bizPost)
    {
        List<BizPost> list = bizPostService.selectBizPostList(bizPost);
        ExcelUtil<BizPost> util = new ExcelUtil<BizPost>(BizPost.class);
        util.exportExcel(response, list, "实习岗位数据");
    }

    /**
     * 获取实习岗位详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizPostService.selectBizPostById(id));
    }

    /**
     * 新增实习岗位
     */
    @PreAuthorize("@ss.hasPermi('cms:post:add')")
    @Log(title = "实习岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPost bizPost)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        bizPost.setUserId(userId);
        return toAjax(bizPostService.insertBizPost(bizPost));
    }

    /**
     * 修改实习岗位
     */
    @PreAuthorize("@ss.hasPermi('cms:post:edit')")
    @Log(title = "实习岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPost bizPost)
    {
        return toAjax(bizPostService.updateBizPost(bizPost));
    }

    /**
     * 删除实习岗位
     */
    @PreAuthorize("@ss.hasPermi('cms:post:remove')")
    @Log(title = "实习岗位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizPostService.deleteBizPostByIds(ids));
    }

    /**
     * 修改实习岗位的开放状态
     */
    @PreAuthorize("@ss.hasPermi('cms:post:edit')")
    @Log(title = "实习岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/editState/{id}")
    public AjaxResult editState(@PathVariable Long id )
    {
        return toAjax(bizPostService.updatePostState(id));
        //return toAjax(bizPostService.updateBizPost(bizPost));
    }

}
